/* $Id:category.cc $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/

#define DEBUG_ROUTINE "category"

#include <sstream>
#include <fstream>
#include "../general/util.h"
#include "../general/log.h"
#include "../general/channel.h"
#include "category.h"
#include "background.h"
#include "../general/general.h"

category::category() {
    nr=0;
    name="";
    age=0;
    level=map<string,class background_level>();
    experience=0;
    armor=0;
}

// Field : nr

const int category::getNr() const { return nr; }
void category::setNr(const int v) { nr=v; }
int category::operator<(const class category &ck) const { return (this->nr < ck.nr)?1:0; }

// Field : name

const string category::getName() const { return name; }
void category::setName(const string v) { name=v; }

// Field : age

const int category::getAge() const { return age; }
void category::setAge(const int v) { age=v; }

// Field : level

class background_level& category::getLevel(const string v) { return level[v]; }
list<class background_level>& category::listLevel() {
    static list<class background_level> res;
    res.clear();
    loopMap(level,v,class background_level) {
        res.push_back(*v);
    }
    res.sort();
    return res;
}
void category::setLevel(class background_level &v) {
    level[v.getBackground().getName()]=v;
}
void category::delLevel(const string v) { level.erase(v); }
const bool category::isLevel(const string v) const { return level.count(v)>0; }

// Field : experience

const int category::getExperience() const { return experience; }
void category::setExperience(const int v) { experience=v; }

// Field : armor

const int category::getArmor() const { return armor; }
void category::setArmor(const int v) { armor=v; }

void category::save(class channel &ch) {
    ch.write("category", "");
    ch.write("n", getNr());
    ch.write("na", getName());
    ch.write("a", getAge());
    loopCategoryLevel(this, s_level) {
        s_level->save(ch);
    }
    ch.write("e", getExperience());
    ch.write("ar", getArmor());
}

void category::load(class channel &ch) {
    if (ch.key=="n" || ch.key=="nr") { setNr(atoi(ch.value.c_str())); ch.next(); }
    if (ch.key=="na" || ch.key=="name") { setName(ch.value); ch.next(); }
    if (ch.key=="a" || ch.key=="age") { setAge(atoi(ch.value.c_str())); ch.next(); }
    while (ch.key=="background_level" || ch.key=="level") {
        ch.next();
        class background_level rec;
        rec.load(ch);
        setLevel(rec);
    }
    if (ch.key=="e" || ch.key=="experience") { setExperience(atoi(ch.value.c_str())); ch.next(); }
    if (ch.key=="ar" || ch.key=="armor") { setArmor(atoi(ch.value.c_str())); ch.next(); }
}

void category::xml(ofstream &f, int l, int r) {
    f << ncpy(l, "  ") << "<category nr=\"" << getNr() <<
    "\" name=\"" << xml_encode(getName()) <<
    "\" age=\"" << getAge() <<
    "\" experience=\"" << getExperience() <<
    "\" armor=\"" << getArmor() <<
    "\">" << endl;
    if (r==0) {
        loopCategoryLevel(this, s_level) {
            s_level->xml(f,l+1);
        }
    }
    f << ncpy(l, "  ") << "</category>" << endl;
}
